package evklid;
import java.util.Scanner;
public class hod
{   static int n=5;
	static int count=1;
    static int [][]arr=new int [n][n];
    static int []dx={2,1,-1,-2,-2,-1,1,2};
    static int []dy={1,2,2,1,-1,-2,-2,-1};
    
	public static void main(String[] args)
  {
		
		for (int i=0; i<n; i++)
		{
			for (int j=0; j<n; j++)
		{
			arr[i][j]=0;
			System.out.print(arr[i][j]+ " ");
		}
		System.out.print("\n");
	    }
		
		int x=(int)Math.random()*(n-1);
		int y=(int)Math.random()*(n-1);
        arr[x][y]=1;
        System.out.println();
        
        for (int i=0; i<n; i++)
		{
			for (int j=0; j<n; j++)
		{
			System.out.print(arr[i][j]+ " ");
		}
		System.out.print("\n");
	    }
        int s=1; 
        
		choose(arr, x, y,s);
	
  }
	
public static void Print()
	{
		 for (int i=0; i<n; i++)
			{
				for (int j=0; j<n; j++)
			{
				System.out.print(arr[i][j]+ "\t");
			}
			System.out.print("\n");
		    }
	}

public static void choose (int arr[][], int x, int y, int s)
  { 
	     arr[x][y]=s;
	if (s==n*n)
	{ 
		Print();
		System.out.println();
	}
	else{
		 if((x+2<n)&&(y+1<n)&&(arr[x+2][y+1]==0)) 
		 {
		     choose(arr, x+2, y+1, s+1);
		 }
		 if((x+1<n)&&(y+2<n)&&(arr[x+1][y+2]==0))
		 {
			 choose(arr, x+1, y+2, s+1);
		 }
		 if((x-1>=0)&&(x-1<n)&&(y+2<n)&&(arr[x-1][y+2]==0))
		 { 
			 choose(arr, x-1, y+2, s+1);
		 }
		 if((x-2>=0)&&(x-2<n)&&(y+1<n)&&(arr[x-2][y+1]==0))
		 { 
			 choose(arr, x-2, y+1, s+1); 
		 }
		 if((x-2>=0)&&(y-1>=0)&&(x-2<n)&&(y-1<n)&&(arr[x-2][y-1]==0)) 
		 {
			 choose(arr, x-2, y-1, s+1);
		 }
		 if((x-1>=0)&&(y-2>=0)&&(x-1<n)&&(y-2<n)&&(arr[x-1][y-2]==0))
		 { 
			 choose(arr, x-1, y-2, s+1);
		 }
		 if((y-2>=0)&&(x+1<n)&&(y-2<n)&&(arr[x+1][y-2]==0)) 
		 {
			 choose(arr, x+1, y-2, s+1);
		 }
		 if((y-1>=0)&&(x+2<n)&&(y-1<n)&&(arr[x+2][y-1]==0))
		 { 
			 choose(arr, x+2, y-1, s+1);
         }
	}
		  arr[x][y]=0;
  }
}
